// pages/visit/visit.js
import {
  visituseradd
} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 表单
    username: "", 
    mobile: "", 
    card: "", 
    carnumber: "京NB2008", 
    starttime: "", 
    endtime: "",
    reason:"",
    department:"",//部门
    travelcode:"1",//行程码
    healthcode:"1",//健康码
    nucleicacid:"1",//核酸
    openid:'',//前台登录获取的openid 
    // 时间
    date: 2002 - 10 - 1,
    datee: 2002 - 10 - 1,
    date: '2022-04-16',
    dateli: '2022-09-09',
    // 当前选中数组
    multiIndex: [0, 0, 0],
    multiArray:[],
    deparment:[],
    thumb:'',
    thumb1:'',
    thumb2:''
  },
  onMyevent(e){
    this.setData({carnumber:e.detail.carnumber})
  },
  bindDateChange(e) {
    // console.log(e)
    this.setData({
      date : e.detail.value
    })
 
  },
  bindDateChangee(e) {
    // console.log(e)
    this.setData({
      datee : e.detail.value
    })
  },
  // 添加行程码图片
  upload(){
    let self=this
    wx.chooseMedia({
      count: 9,
      mediaType: ['mix'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        // console.log(res);
       let filepath= res.tempFiles[0].tempFilePath
        let token =wx.getStorageSync('token')
            wx.uploadFile({
              url: 'http://121.42.230.80/api/minivisit/upload', //仅为示例，非真实的接口地址
              filePath: filepath,
              header:{
                token:token
              },
              name: 'file',
              formData: {},
              success (res){
                // console.log(res);
                let thum = res.data
                let pu = JSON.parse(thum)
                // console.log(pu)
                let thumb = pu.data.src
                self.setData({
                  thumb:thumb
                })

                //do something
              }
            })
      }
    }) 
// console.log(1222);
  },
   // 添加健康宝图片
   upload1(){
    let self=this
    wx.chooseMedia({
      count: 9,
      mediaType: ['mix'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        // console.log(res);
       let filepath= res.tempFiles[0].tempFilePath
        let token =wx.getStorageSync('token')
            wx.uploadFile({
              url: 'http://121.42.230.80/api/minivisit/upload', //仅为示例，非真实的接口地址
              filePath: filepath,
              header:{
                token:token
              },
              name: 'file',
              formData: {},
              success (res){
                // console.log(res);
                let thum = res.data
                let pu = JSON.parse(thum)
                // console.log(pu)
                let thumb1 = pu.data.src
                self.setData({
                  thumb1:thumb1
                })

                //do something
              }
            })
      }
    }) 
// console.log(1222);
  },
   // 添加核算报告图片
   upload2(){
    let self=this
    wx.chooseMedia({
      count: 9,
      mediaType: ['mix'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        // console.log(res);
       let filepath= res.tempFiles[0].tempFilePath
        let token =wx.getStorageSync('token')
            wx.uploadFile({
              url: 'http://121.42.230.80/api/minivisit/upload', //仅为示例，非真实的接口地址
              filePath: filepath,
              header:{
                token:token
              },
              name: 'file',
              formData: {},
              success (res){
                // console.log(res);
                let thum = res.data
                let pu = JSON.parse(thum)
                // console.log(pu)
                let thumb2 = pu.data.src   
                // console.log(thumb2)
                self.setData({
                  thumb2:thumb2
                })

                //do something
              }
            })
      }
    }) 
// console.log(1222);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getdepartmentlist()
  },
  // 入校时间
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 离校时间
  bindDateChangeli: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      dateli: e.detail.value
    })
  },
  // 部门
  bindMultiPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let selectval= e.detail.value
    //根据下标找对应的数据库id
    // console.log(this.data.multiArray);
    let onename= this.data.multiArray[0][selectval[0]];
    // console.log(onename)
    let twoname= this.data.multiArray[1][selectval[1]];
    // console.log(twoname)
    let threename= this.data.multiArray[2][selectval[2]];
    // console.log(threename)

    this.data.deparment.map((v,i)=>{
      // console.log(v)
      v.son.map((vv,ii)=>{
        
        // console.log(vv);
        vv.son.map((vvv,iii)=>{
          
          if(onename==v.name && twoname==vv.name && threename==vvv.name){
            // console.log(vvv.id)
            this.setData({department:vvv.id})
          }
        })
      })
   
    })

    this.setData({
      multiIndex: e.detail.value
    })
  },
  // 部门
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        // console.log('第一列 请求第二列的数据',data.multiIndex[0],this.data.multiArray[e.detail.column][e.detail.value]);
          let name= this.data.multiArray[e.detail.column][e.detail.value]
          // console.log(name);
          data.multiArray[1]=this.getdata(name)
          this.setData({
            name:name
          })
          //默认值
          let parentname1 =this.data.name;
          let currentname1 = this.data.multiArray[1][0];
          data.multiArray[2]=this.gettwodata(parentname1,currentname1)
        break;
      case 1:
        // console.log('第二列请求第三列数据',data.multiIndex[1]);
        let parentname =this.data.name;
        let currentname = this.data.multiArray[e.detail.column][e.detail.value];
        data.multiArray[2]=this.gettwodata(parentname,currentname)
        break;
    }
    this.setData(data);
  },
  getdata(name){
    let arr = []
    this.data.deparment.map((v,i)=>{
       if(v.name==name){
          arr= v.son.map((vv,ii)=>{
            return vv.name
          })
       }
    })
    return arr;
  },
  gettwodata(parentname,currentname){
    let arr = []
    this.data.deparment.map((v,i)=>{
        v.son.map((vv,ii)=>{
          if(v.name==parentname && vv.name==currentname){
            arr= vv.son.map(item=>item.name)
          }    
        })
    })
    return arr;
  },
  getdepartmentlist(){
    let that = this
    let token = wx.getStorageSync('token');
    wx.request({
      url: 'http://121.42.230.80/api/minivisit/departmentlist', //仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json',
        'Authorization':token
      },
      success (res) {

        if (res.data.code == '00000') {
          that.setData({
            deparment:res.data.data
          })
        }
        that.getmultiArray()
      }
    })
  },
  getmultiArray(){
    if(!this.data.deparment){
      return
    }
    let deparment = this.data.deparment
    let arr1=[]
    let arr2=[]
    let arr3=[]
    deparment.forEach((v,i)=>{
      arr1.push(v.name)
      if(i==0){
        this.data.name=v.name
        v.son.forEach((vv,ii)=>{
          arr2.push(vv.name)
          if(ii==0){
           vv.son.forEach((vvv,iii)=>{
            //  console.log(vvv,'==================')
              arr3.push(vvv.name)
           })
          }
        })
      }
    })
    let data=[arr1,arr2,arr3];
    this.setData({
      multiArray:data
    })
  },
  // 访客登记
  async formSubmit(e){
    let openid = wx.getStorageSync('UserInfo').openid;
    let data = {
      username:e.detail.value.username,
      mobile:e.detail.value.mobile,
      card:e.detail.value.card,
      carnumber:this.data.carnumber,
      starttime:this.data.date,
      endtime:this.data.dateli,
      reason:e.detail.value.reason,
      department:this.data.department,
      travelcode:this.data.thumb,
      healthcode:this.data.thumb1,
      nucleicacid:this.data.thumb2,
      openid:openid
    }
    // console.log(data,'==-');
    let res = await visituseradd(data);
    // console.log('===',res);
    if (res.code=='00000') {
      wx.navigateTo({
        url: '/pages/myreservation/myreservation?openid='+openid,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})